package com.gack.business.dao;

import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * 
* @ClassName: StoreBookingDetailDao 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月11日 下午2:51:38 
* @version V1.0
 */
@Repository
public class StoreBookingDetailDao {
	@Autowired
	private EntityManager entityManager;
	
	/**
	 * 获取门店预约详情
	 */
	public StoreBookingDetail getStoreBookingDetail(String orderid){
		StoreBookingDetail storeBookingDetail = null;
		String jpql = "select"
				+ " o.id,o.order_id as orderid,u.nickname as username,o.created_time as reservedtime,o.status,o.cancel_reason as reservedreason"
				+ " from orders o "
				+ " LEFT JOIN user u on o.user_id = u.id"
				+ " where o.id = :orderid";
		Query query = entityManager.createNativeQuery(jpql,StoreBookingDetail.class);
		query.setParameter("orderid", orderid);
		storeBookingDetail = (StoreBookingDetail)query.getSingleResult();
//		if(storeBookingDetail != null && storeBookingDetail.getStatus().equals("1")){
//			jpql = "select reason from cancel_orderform_reason where orderform_id = :orderid";
//			query = entityManager.createNativeQuery(jpql,StoreBookingDetail.class);
//			query.setParameter("orderid", storeBookingDetail.getId());
//			List<String> reasons = query.getResultList();
//			if(reasons != null && reasons.size() > 0){
//				storeBookingDetail.setReservedreason(reasons.get(0));
//			}
//		}
		return storeBookingDetail;
	}
	
	/**
	 * 获取门店预约列表
	 */
	public List<StoreBookingDetail> getStoreReservationList(String storeid,String beginTime,String endTime,Integer page,Integer size){
		List<StoreBookingDetail> storeBookingDetails = null;
		String jpql = "select"
				+ " o.id,o.order_id as orderid,u.nickname as username,o.created_time as reservedtime,o.status,o.cancel_reason as reservedreason"
				+ " from orders o "
				+ " LEFT JOIN user u on o.user_id = u.id"
				+ " WHERE 1=1"
				+ " and o.status in ('1','2','3')"
				+ " and o.store_id = :storeid";
		if(beginTime != null && !beginTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(beginTime.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				beginTime = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql +=" and o.created_time > '"+beginTime+"'";
		}
		if(endTime != null && !endTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(endTime.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				endTime = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql +=" and o.created_time < '"+endTime+"'";
		}
		Query query = entityManager.createNativeQuery(jpql,StoreBookingDetail.class);
		query.setParameter("storeid", storeid);
		storeBookingDetails = query.setFirstResult(page*size).setMaxResults(size).getResultList();
		return storeBookingDetails;
	}
	
	/**
	 * 获取门店预约列表total
	 */
	public long getStoreReservationTotal(String storeid,String beginTime,String endTime,Integer page,Integer size){
		BigInteger total = new BigInteger("0");
		String jpql = "select"
				+ " count(o.id)"
				+ " from orders o "
				+ " LEFT JOIN user u on o.user_id = u.id"
				+ " WHERE 1=1"
				+ " and o.status in ('1','2','3')"
				+ " and o.store_id = :storeid";
		if(beginTime != null && !beginTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(beginTime.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				beginTime = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql +=" and o.created_time > '"+beginTime+"'";
		}
		if(endTime != null && !endTime.equals("")){
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS Z");
			try {
				Date d = format.parse(endTime.replace("Z", " UTC"));
				format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				endTime = format.format(d);
			} catch (ParseException e) {
				e.printStackTrace();
			}//注意是空格+UTC
			jpql +=" and o.created_time < '"+endTime+"'";
		}
		Query query = entityManager.createNativeQuery(jpql);
		query.setParameter("storeid", storeid);
		total = (BigInteger)query.getSingleResult();
		return total.intValue();
	}
}
